﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace He.Web.manage.tools
{
    public partial class txt_replace : He.Web.UI.Manage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack) 
            {
                chkLoginLevel("txtReplace");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string oldTxt = this.txtOldTxt.Text.Trim();
            string newTxt = this.txtNewTxt.Text.Trim();

            //替换网站信息
            BLL.config configbll = new He.BLL.config();
            BLL.site_info sbll = new BLL.site_info();
            Model.site_info site_info;
            List<Model.Lang> lst = new BLL.config().LoadConfig<List<Model.Lang>>(LangXmlPath);
            foreach (Model.Lang item in lst)
            {
                if (item.IsShow)
                {
                    site_info = sbll.GetModelByLang(item.Value);
                    site_info.WebName = site_info.WebName.Replace(oldTxt, newTxt);
                    siteinfo.WebTitle = siteinfo.WebTitle.Replace(oldTxt, newTxt);
                    site_info.WebAuthor = site_info.WebAuthor.Replace(oldTxt, newTxt);
                    site_info.WebKeyWords = site_info.WebKeyWords.Replace(oldTxt, newTxt);
                    site_info.WebDescription = site_info.WebDescription.Replace(oldTxt, newTxt);
                    string filePath = string.Format("~/config/lang_{0}.config", item.Value);
                    configbll.SaveConfig<He.Model.site_info>(site_info, filePath);
                }
            }
            //替换文章栏目
            BLL.article abll = new BLL.article();
            List<Model.article> articles = abll.GetModelList("id>0");
            foreach (Model.article item in articles)
            {
                item.title = item.title.Replace(oldTxt, newTxt);
                item.seo_keywords = item.seo_keywords.Replace(oldTxt, newTxt);
                item.seo_desc = item.seo_desc.Replace(oldTxt, newTxt);
                item.content = item.content.Replace(oldTxt, newTxt);
                abll.Update(item);
            }
            //替换分类
            BLL.category catebll = new BLL.category();
            List<Model.category> categorys = catebll.GetModelList("id>0");
            foreach (Model.category item in categorys)
            {
                item.title = item.title.Replace(oldTxt, newTxt);
                item.seo_keywords = item.seo_keywords.Replace(oldTxt, newTxt);
                item.seo_desc = item.seo_desc.Replace(oldTxt, newTxt);
                item.content = item.content.Replace(oldTxt, newTxt);
                catebll.Update(item);
            }
            SaveLogs("[系统管理]文本批量替换");
            JscriptPrint("批量替换成功啦！", Request.RawUrl, "Success");

        }
    }
}